 {extend name="v1/layout/base"}
 {block name="content"}
 <div class="layuimini-main">

   <fieldset class="table-search-fieldset xk-search-status" id="xk-search-status">
     <legend>搜索信息</legend>
     <div style="margin: 10px 10px 10px 10px">
       <div class="layui-row layui-col-space15">
         <div class="layui-col-md12">
           <form class="layui-form">
             <div class="layui-card-body layui-form layui-form-pane">
               <div class="layui-row layui-col-space10 layui-form-item">
                 <div class="layui-col-lg6">
                   <label class="layui-form-label">搜索关键词</label>
                   <div class="layui-input-block">
                     <input type="text" name="keywords" placeholder="请输入搜索关键词" id="keywords" class="layui-input">
                   </div>
                 </div>
                 <div class="layui-col-lg6">
                   <label class="layui-form-label">所属仓库</label>
                   <div class="layui-input-block">
                     {volist name="$store_list" id="vo"}
                     <input type="checkbox" class="layui-input" lay-skin="primary" name="store_id" value="{$vo.store_id}" title="{$vo.store_name}">
                     {/volist}
                   </div>
                 </div>
               </div>
               <div class="layui-row layui-col-space10 layui-form-item">
                 <div class="layui-col-lg6">
                   <label class="layui-form-label">选择省</label>
                   <div class="layui-input-block province">
                     <select id="province_show" lay-filter="provinceSelect" lay-search>
                       <option value="">请选择省</option>
                     </select>
                     <input type="hidden" id="province" value="">
                   </div>
                 </div>
                 <div class="layui-col-lg6">
                   <label class="layui-form-label">选择市</label>
                   <div class="layui-input-block">
                     <select id="city_show" lay-filter="citySelect"  lay-search>
                       <option value="">请选择市</option>
                     </select>
                     <input type="hidden" id="city" value="">
                   </div>
                 </div>
               </div>
               <div class="layui-row layui-col-space10 layui-form-item">
                 <div class="layui-col-lg12">
                   <label class="layui-form-label">确认收货时间</label>
                   <div class="layui-input-block">
                     <input type="text" class="layui-input" autocomplete="off" id="search_time" placeholder="请选择对应的确认收货时间范围">
                   </div>
                 </div>
               </div>
               <div class="layui-row">
                 <div class="layui-col-xs4">&nbsp;</div>
                 <div class="layui-col-xs4">
                   <button type="button" class="layui-btn layui-btn-primary layui-btn-radius close-search"><i class="layui-icon">&#x1007;</i>关闭</button>
                   <button type="button" class="layui-btn layui-btn-normal layui-btn-radius xk-dosearch">搜索<i class="layui-icon"></i></button>
                 </div>
                 <div class="layui-col-xs4">&nbsp;</div>
               </div>
             </div>
           </form>
         </div>
       </div>
     </div>
   </fieldset>

   <script type="text/html" id="toolbar">
     <div class="layui-btn-container">
       <button class="layui-btn layui-btn-black  layui-btn-sm" lay-event="reseve"><i class="layui-icon">&#xe63d;</i></button>
       <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="export"><i class="layui-icon">&#xe67d;</i>导出数据</button>
       <div class="money_all"></div>
     </div>
   </script>

   <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

   <script type="text/html" id="currentTableBar">
     <button class="layui-btn layui-btn-radius layui-btn-warm layui-btn-sm" lay-event="show" data-url="/{:config('manage.manage_name')}/{:config('manage.version')}/Goods/statistics"><i class="layui-icon">&#xe642;</i>查看详情</button>
   </script>
   <!-- 图片 如果存在的话 -->
   <script type="text/html" id="picurlshow">
     <img src="{{d.picurl?d.picurl:'__COMMON__images/no_pic.png'}}" style="width:100px; height:100px" />
   </script>
   <!-- 状态按钮 -->
 </div>
 {/block}
 {block name="myScript"}
 <style media="screen">
   .layui-table-cell {
     height: 100px;
     line-height: 100px;
   }
 </style>
 <script>
   layui.use(['form', 'table', 'laydate'], function() {
     var $ = layui.jquery,
       form = layui.form,
       laydate = layui.laydate,
       table = layui.table;
     laydate.render({
       elem: '#search_time',
       range: '~',
       theme: '#1E9FFF',
       type: 'datetime',
       format: 'yyyy-MM-dd HH:mm:ss'
     });
     table.render({
       elem: '#currentTableId',
       url: '/{:config("manage.manage_name")}/{:config("manage.version")}/Goods/statistics',
       toolbar: '#toolbar',
       defaultToolbar: ['filter', {
         title: '搜索',
         layEvent: 'search',
         icon: 'layui-icon-search'
       }],
       cols: [
         [{
             field: 'picurl',
             width: '10%',
             title: '产品图片',
             templet: "#picurlshow"
           },
           {
             field: 'goods_name',
             width: '10%',
             title: '产品名称',
           },
           {
             field: 'spec_value',
             width: '15%',
             title: '规格'
           },
           {
             field: 'all_count',
             width: '10%',
             title: '确认收货量'
           },
           {
             field: 'all_price',
             width: '10%',
             title: '确认收货金额'
           },
           {
             field: 'first_count',
             width: '10%',
             title: '首购数量'
           },
           {
             field: 'first_price',
             width: '10%',
             title: '首购金额'
           },
           {
             field: 'next_count',
             width: '10%',
             title: '复购数量'
           },
           {
             field: 'next_price',
             width: '15%',
             title: '复购金额',
             minWidth: '200',
             fixed: 'right',
           }
         ]
       ],
       done: function(res, curr, count) {
         $(".money_all").html(res.resultData.show_count);
       },
       limits: [20, 150, 200, 250, 500, 1000],
       limit: 20,
       id: 'table_ids',
       page: true,
       skin: 'line'
     });

     // 顶部操作工具
     table.on('toolbar(currentTableFilter)', function(obj) {
       var url = $(this).attr('data-url'),
         id_arr = getCheckValue();
         var keywords = $("#keywords").val(),
           search_time = $("#search_time").val(),
           province = $("#province").val(),
           city = $("#city").val(),
           store_id = '',
           store_id_arr = [];
         $("input[name=store_id]:checked").each(function() {
           store_id_arr.push($(this).val());
         });
         store_id = store_id_arr.join(',');
       switch (obj.event) {
         case 'reseve':
           table.reload('table_ids');
           break;
         case 'export':
           var url = "/{:config('manage.manage_name')}/{:config('manage.version')}/Goods/exportStatistics";
           https(url, {
             keywords: keywords,
             search_time: search_time,
             province: province,
             city: city,
             store_id: store_id
           }, 'post', true, '正在执行订单数据导出').then(res => {
             if (res.result_code == 200) {
               var html = '<h1 style="padding: 0 20px;color:red;font-size:18px;margin-bottom:15px;">如果据量大于1万条系统自动分表导出</h1>';
               res.resultData.forEach(function(value, index) {
                 html += '<h2 style="padding: 0 20px;font-size:16px;">第' + (index + 1) + '个订单文件：<a href="' + value + '">' + '下载' + '</a></h2>';
               })
               layer.open({
                 type: 1,
                 skin: 'layui-layer-rim', //加上边框
                 area: ['400px', '200px'], //宽高
                 content: html
               });
             }
           })
           break;
         case 'search':
           $("#xk-search-status").removeClass('xk-search-status');
           break;
       }
     })
     // 列表工具栏目
     table.on('tool(currentTableFilter)', function(obj) {
       var data = obj.data,
         event = obj.event,
         url = $(this).attr('data-url');
       url = url + '/goods_id/' + data.goods_id;
       var keywords = $("#keywords").val(),
         search_time = $("#search_time").val(),
         province = $("#province").val(),
         city = $("#city").val(),
         store_id = '',
         store_id_arr = [];
       $("input[name=store_id]:checked").each(function() {
         store_id_arr.push($(this).val());
       });
       store_id = store_id_arr.join(',');
       switch (event) {
         case 'show':
           var index = layer.open({
             title: '产品' + data.goods_name + '编辑',
             type: 2,
             shade: 0.2,
             maxmin: true,
             shadeClose: true,
             area: ['100%', '100%'],
             content: url,
             cancel: function() {
               table.reload('table_ids', {
                 where: {
                   keywords: keywords,
                   search_time: search_time,
                   province: province,
                   city: city,
                   store_id: store_id
                 }
               });
             }
           });
           break;
       }
     })
     // 执行搜索
     $(".xk-dosearch").click(function() {
       var keywords = $("#keywords").val(),
         search_time = $("#search_time").val(),
         province = $("#province").val(),
         city = $("#city").val(),
         store_id = '',
         store_id_arr = [];
       $("input[name=store_id]:checked").each(function() {
         store_id_arr.push($(this).val());
       });
       store_id = store_id_arr.join(',');
       table.reload('table_ids', {
         page: {
           curr: 1
         },
         where: {
           keywords: keywords,
           search_time: search_time,
           province: province,
           city: city,
           store_id: store_id
         }
       }, 'data');
     })
     // 获取选中项
     function getCheckValue() {
       var id_arr = [],
         id_str = '';
       var checkStatus = table.checkStatus('table_ids'),
         data = checkStatus.data;
       data.map(function(d) {
         id_arr.push(d.goods_id);
       })
       id_str = id_arr.join(',');
       return id_str;
     }
     getProvince();
     // 第一步 获取省的数据
     function getProvince() {
       var url = "/{:config('manage.manage_name')}/{:config('manage.version')}/Goods/getArea";
       https(url, {
         p_code: 100000
       }, false, '').then(res => {
         var html = '<option value="">请选择省</option>';
         if (res.resultData.list) {
           res.resultData.list.forEach((item, i) => {
             html += '<option value="' + item.code + '" data-name="'+item.name+'">' + item.name + '</option>';
           });
         }
         $("#province_show").html(html);
         form.render();
       }, error => {

       })
     }
     // 第二步 获取市的数据
     form.on('select(provinceSelect)', function(data) {
       $("#province").val(data.elem[data.elem.selectedIndex].text);
       $("#city").val('');
       getCity(data.value);
     })
     function getCity(p_code) {
       var url = "/{:config('manage.manage_name')}/{:config('manage.version')}/Goods/getArea";
       https(url, {
         p_code: p_code
       }, false, '').then(res => {
         var html = '<option value="">请选择市</option>';
         if (res.resultData.list) {
           res.resultData.list.forEach((item, i) => {
             html += '<option value="' + item.code + '" data-name="'+item.name+'">' + item.name + '</option>';
           });
         }
         $("#city_show").html(html);
         form.render();
       }, error => {

       })
     }
     form.on('select(citySelect)', function(data) {
       $("#city").val(data.elem[data.elem.selectedIndex].text);
     })
   });
 </script>
 {/block}
